package com.ruoyi.basic.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.domain.BasicWork;
import com.ruoyi.basic.service.IBasicUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.BasicEducate;
import com.ruoyi.basic.service.IBasicEducateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教育经历Controller
 * 
 * @author ruoyi
 * @date 2021-08-22
 */
@Controller
@RequestMapping("/basic/educate")
public class BasicEducateController extends BaseController
{
    private String prefix = "basic/educate";

    @Autowired
    private IBasicEducateService basicEducateService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("basic:educate:view")
    @GetMapping()
    public String educate()
    {
        return prefix + "/educate";
    }

    /**
     * 查询教育经历列表
     */
    @RequiresPermissions("basic:educate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicEducate basicEducate)
    {
        startPage();
        List<BasicEducate> list = basicEducateService.selectBasicEducateList(basicEducate);
        return getDataTable(list);
    }

    /**
     * 导出员工教育经历
     */
    @RequiresPermissions("basic:educate:export")
    @Log(title = "员工教育经历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicEducate basicEducate)
    {
        List<BasicEducate> list = basicEducateService.selectBasicEducateList(basicEducate);
        ExcelUtil<BasicEducate> util = new ExcelUtil<BasicEducate>(BasicEducate.class);
        return util.exportExcel(list, "员工教育经历信息数据");
    }

    @Log(title = "员工教育经历", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:educate:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicEducate> util = new ExcelUtil<BasicEducate>(BasicEducate.class);
        List<BasicEducate> educateList = util.importExcel(file.getInputStream());
        String message = basicEducateService.importEducate(educateList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:educate:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicEducate> util = new ExcelUtil<BasicEducate>(BasicEducate.class);
        return util.importTemplateExcel("员工教育经历信息");
    }

    /**
     * 新增教育经历
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BasicUser user = basicUserService.selectBasicUserById(userId);
        mmap.put("user", user == null ? new BasicUser() : user);
        return prefix + "/add";
    }

    /**
     * 新增保存教育经历
     */
    @RequiresPermissions("basic:educate:add")
    @Log(title = "教育经历", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicEducate basicEducate)
    {
        basicEducate.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicEducateService.insertBasicEducate(basicEducate));
    }

    /**
     * 修改教育经历
     */
    @GetMapping("/edit/{educateId}")
    public String edit(@PathVariable("educateId") Long educateId, ModelMap mmap)
    {
        BasicEducate basicEducate = basicEducateService.selectBasicEducateByEducateId(educateId);
        mmap.put("basicEducate", basicEducate);
        return prefix + "/edit";
    }

    /**
     * 修改保存教育经历
     */
    @RequiresPermissions("basic:educate:edit")
    @Log(title = "教育经历", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicEducate basicEducate)
    {
        basicEducate.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicEducateService.updateBasicEducate(basicEducate));
    }

    /**
     * 删除教育经历
     */
    @RequiresPermissions("basic:educate:remove")
    @Log(title = "教育经历", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicEducateService.deleteBasicEducateByEducateIds(ids));
    }
}
